package com.link.base.base.serviceprojectnew.service;

import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.model.FlowType;
import com.link.base.base.approval.service.FlowService;
import com.link.base.user.model.CoreUser;
import com.link.core.util.UserUtil;
import com.link.base.base.serviceprojectnew.dao.mybatis.mapper.PublishProgramMapper;
import com.link.base.base.serviceprojectnew.model.PublishProgram;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 发布程序审核
 */
@Service
public class PublishProgramServiceImpl extends BasicServiceImpl<PublishProgram> implements PublishProgramService {

    @Resource
    private PublishProgramMapper publishProgramMapper;

    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<PublishProgram> getBasicMapper() {
        return publishProgramMapper;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void submitPublishProgram(PublishProgram record, Long selectUserId) throws Exception {
        PublishProgram validatePublishProgram = publishProgramMapper.queryById(record);
        if (!StringUtils.equals(validatePublishProgram.getPublishStatus(),
                "No Published")) {
            throw new ServiceException("SERVICEREQUESTNEW-002");
        }
        record.setPublishStatus("Submitted");
        publishProgramMapper.submitPublishProgram(record);
        flowService.createFlow(record.getId(), FlowType.PUBLISH_PROGRAM_SUBMIT, selectUserId, "发布程序:" + validatePublishProgram.getName(),0L,"");
    }

    @Override
    public void approvalUpdatePublishProgramStatus(Long objectId, String param1, String param2, String param3,
                                                   String param4, String param5) throws Exception {
        CoreUser user = UserUtil.getUser();
        PublishProgram queryPublishProgram = new PublishProgram();
        queryPublishProgram.setId(objectId);
        PublishProgram publishProgram = queryById(queryPublishProgram);
        if (publishProgram == null) {
            throw new ServiceException("SERVICEREQUESTNEW-003");
        }
        //根据对象id取审批流程
        Flow queryFlow = new Flow();
        queryFlow.setFlowObjId(objectId);
        List<Flow> flowList = flowService.queryFlowByObjId(queryFlow);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Flow lastFlow = new Flow();
        int size = 2;
        if (flowList.size() >= size) {
            for (int i = 0; i < flowList.size() - 1; i++) {
                if (sdf1.parse(flowList.get(i).getLastUpdated()).getTime() < sdf2.parse(flowList.get(i + 1).getLastUpdated()).getTime()) {
                    lastFlow = flowList.get(i + 1);
                } else {
                    lastFlow = flowList.get(i);
                }
            }
        } else if (flowList.size() == 1) {
            lastFlow = flowList.get(0);
        }

        //审批拒绝
        String noPublished = "No Published";
        if (noPublished.equals(param1)) {
            PublishProgram record = new PublishProgram();
            record.setId(objectId);
            record.setPublishStatus(param1);

            record.setLastAuditorName(user.getFirstName());
            record.setAuditHistoryId(lastFlow.getId());

            publishProgramMapper.publishProgramStatusUpdate(record);
        }
        //审批通过
        String pendingPublished = "Pending Published";
        if (pendingPublished.equals(param1)) {
            PublishProgram record = new PublishProgram();
            record.setId(objectId);
            record.setPublishStatus(param1);

            record.setLastAuditorName(user.getFirstName());
            record.setAuditHistoryId(lastFlow.getId());

            publishProgramMapper.publishProgramStatusUpdate(record);
        }

    }

    @Override
    public void updatePublishStatus(PublishProgram record) throws Exception {
        publishProgramMapper.publishProgramStatusUpdate(record);

    }
}
